﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using cecbData;
using cecbClasses.Data_Adapters;
using System.Windows.Media.Animation;

namespace CECB.Impact
{
    /// <summary>
    /// Interaction logic for ProjectImpactsUpdate_Window.xaml
    /// </summary>
    public partial class ProjectImpactsUpdate_Window : Window
    {
        public ProjectImpactsUpdate_Window()
        {
            InitializeComponent();
            cecbContext = new DSSEntities();
            ProjectAutocompleteTextBox.ItemsSource = cecbContext.Projects;
            textBoxImpactSearch.IsEnabled = false;
    
        }

        string impact;
        string impactReference;
        DSSEntities cecbContext;
        Projects proObj = null;
        Boolean isProjectSelected = false;


        private void ProjectAutocomplete_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (((AutoCompleteBox)sender).SelectedItem != null)
            {
                proObj = (Projects)ProjectAutocompleteTextBox.SelectedItem;
                ProjectDetailsWrapPanel.DataContext = proObj;
                refreshImpactsList();
                isProjectSelected = true;
                textBoxImpactUpdate.Clear();
                textBoxImpactSearch.IsEnabled = true;
                labelProjectCat.Content = proObj.Categories.cat_name;
                labelProjectName.Content = proObj.proj_name;
                labelProjectTeam.Content = proObj.proj_team;
            }
            else
            {
                isProjectSelected = false;
                textBoxImpactSearch.IsEnabled = true;
                labelProjectCat.Content = "";
                labelProjectName.Content = "";
                labelProjectTeam.Content = "";
                listBoxImpacts.ItemsSource = null;
                listBoxImpacts.Items.Clear();
            }
        }

        private void comboBoxComponents_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (isProjectSelected)
            {
                MessageBox.Show("Select a project", "Message", MessageBoxButton.OK, MessageBoxImage.Error);

            }
            else
            {

                cecbClasses.Data_Adapters.ImpactAndActivity.Func_Impacts impacts = new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Impacts();
               
                listBoxImpacts.Visibility = Visibility.Visible;
                (FindResource("drop") as Storyboard).Begin(this);
            }
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            if (ProjectAutocompleteTextBox.SelectedItem == null)
            {
                MessageBox.Show("Select a project", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

            }
            else
            {
                cecbClasses.Data_Adapters.ImpactAndActivity.Func_Impacts impacts = new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Impacts();
                
                if (textBoxImpactUpdate.Text == "")
                {
                    MessageBox.Show("Enter updated impact", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

                }
                else if (old == textBoxImpactUpdate.Text) {

                    MessageBox.Show("Nothing has been modified", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    if (impacts.updateImpactsDetails(impactReference.ToString(), textBoxImpactUpdate.Text.ToString()))
                    {
                        MessageBox.Show("Project Impact Updated Successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                        textBoxImpactUpdate.Clear();
                        labelProjectCat.Content = "";
                        labelProjectName.Content = "";
                        labelProjectTeam.Content = "";
                    }
                    else
                    {
                        MessageBox.Show("Project Impact Updation failed", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    //listBoxActivities.ItemsSource = impacts.getImpacts(proObj.proj_reference, comboBoxComponents.Text);

                }
            }
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            if (isProjectSelected)
            {
                if (listBoxImpacts.SelectedIndex > -1)
                {

                    if (new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Impacts().checkImpactIsAllowedToDelete(getImpact))
                    {

                        if (MessageBox.Show("You are about to delete a impact from  ' " + ProjectAutocompleteTextBox.Text + "' project. Confirm to continue?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                        {
                            if (new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Impacts().deleteProjectImpact(getImpact, false))
                                MessageBox.Show("Project Impact Deleted Successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                            else
                            {
                                if (MessageBox.Show("You cannot delete this impact directly. Values are added to matrix using this impact. If you delete this impact, matrix values will be lost. Do you need to continue? ", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                                {

                                    if (new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Impacts().deleteProjectImpact(getImpact, true))
                                    {
                                        MessageBox.Show("Project Impact Deleted Successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                                        textBoxImpactUpdate.Clear();
                                        labelProjectCat.Content = "";
                                        labelProjectName.Content = "";
                                        labelProjectTeam.Content = "";
                                    }
                                }

                            }
                            
                            textBoxImpactUpdate.Clear();
                            refreshImpactsList();
                        }
                    }
                    else { MessageBox.Show("You cannot delete this impact. Emails have been sent to consultants using this impact", "Message", MessageBoxButton.OK, MessageBoxImage.Error); }
                }
                else {
                    MessageBox.Show("Select a impact", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else MessageBox.Show("Select a project", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }
        String old = "";
        ProjectImpacts getImpact;


        private void listBoxActivities_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listBoxImpacts.Items.Count > 0)
            {
                try
                {
                    getImpact = (ProjectImpacts)listBoxImpacts.SelectedItem;
                    textBoxImpactUpdate.Text = getImpact.Impacts.impt_name.ToString();

                }
                catch (Exception)
                {
                    return;

                }
            }
             
        }

 
        private void textBoxImpactSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            listBoxImpacts.ItemsSource = new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Impacts().getProjectImpactDetailsByName(textBoxImpactSearch.Text,proObj);
            (FindResource("drop") as Storyboard).Begin(this);
        }

        private void refreshImpactsList() {

            listBoxImpacts.ItemsSource = null;
            listBoxImpacts.Items.Clear();
            listBoxImpacts.ItemsSource = from c in new DSSEntities().ProjectImpacts
                                            where c.proj_reference.Equals(proObj.proj_reference)
                                            select c;
            listBoxImpacts.Visibility = System.Windows.Visibility.Visible;
            (FindResource("drop") as Storyboard).Begin(this);
        }
    }
}